/**
 * Environments
 */
jsx3.lang.Package.definePackage("tibco.admin.environments", function (ep) {
});

/**
 * Environments
 */
jsx3.lang.Class.defineClass("tibco.admin.environments.approve",
        jsx3.lang.Object,
        [tibco.admin.plugin.SelectPerspectiveInterface, tibco.admin.plugin.PluginContainerInterface],
        function (plugin) {

            plugin.instance = null;
            plugin.prototype.buttonPlugins = new Object();
            plugin.prototype.buttonsToLoad = new Object();
            plugin.prototype.envId = null;
            plugin.prototype.type = "approve";

            plugin.prototype.init = function() {
                this.jsxsuper();
                tibco.admin.Main.loadResource("tibco/admin/environments/jss/jss.xml", "tibco.admin", "jss30", "ljss", false);
                tibco.admin.Main.loadResource("tibco/admin/environments/jss/xslt.xml", "tibco.admin", "xslt30", "jss", false);
            }

            plugin.prototype.getNamespace = function() {
                return "tibco.admin";
            }

            plugin.prototype.destroyPlugin = function() {
            }

            plugin.getInstance = function() {
                if(!this.instance) {
                    this.instance = new tibco.admin.environments.approve();
                }
                return this.instance;
            }

            plugin.prototype.getIconPath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@Welcome Env Icon");
            }

            plugin.prototype.getDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@welPageCfgEnvContent_00");
            }

            plugin.prototype.getSelectText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@SelectText_2");
            }
            
            plugin.prototype.filter = function(id) {
				var targetMatrix = this.block.getDescendantOfName("mtxApproves");
                targetMatrix.resetXmlCacheData(tibco.admin.Main.getServer());
                targetMatrix.setXMLString(this.cacheDoc.toString());
                var iter = targetMatrix.getXML().getChildIterator();
                while(iter.hasNext()) {
                    var obj = iter.next();
                    if(id!=obj.getAttribute("Status") && id!="全部") {
                        targetMatrix.deleteRecord(obj.getAttribute("jsxid"),false);
                    }
                }
                targetMatrix.repaintData();
            }

            plugin.prototype.dobuildNodes = function(parent) {
                if(this.nodeIds && (this.nodeIds.length > 0)) {
                    try {
                        var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/rules/buildNodes.xml", "buildNodes");
                        if(tibco.admin.Main.getServer().getEnv("MODE")) {
                            objService.setEndpointURL(tibco.admin.util.ServiceHelper.getNodeEndpointURL());
                        }
                        var nodeList = new jsx3.util.List(0);
                        var recordIter = this.matrixObj.getSelectedNodes().iterator();
                        while(recordIter.hasNext()) {
                            var record = recordIter.next();
                            var obj = new Object();
                            obj.nodeId = record.getAttribute("jsxid");
                            obj.nodeName = record.getAttribute("nodeName");
                            nodeList.add(obj, nodeList.size());
                        }
                        objService.nodesIter = nodeList.iterator();

                        //subscribe and call
                        var me = this;
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onbuildNodesSuccess);
                        objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onbuildNodesError);
                        objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onbuildNodesInvalid);
                        objService.doCall();
                    }
                    catch(ex) {
                        tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.Environments.dobuildNodes");
                    }
                }
            }

            plugin.prototype.yftPrompt = function(strMessage) {
                var lblFadeMessage = this.block.getDescendantOfName("lblFadeMessage_30");
                lblFadeMessage.setText(strMessage, true);
                yft.startFadeOut(lblFadeMessage, 15, 1250);
            }

            plugin.prototype.yftStop = function() {
                yft.stopFadeOut();
            }

            plugin.prototype.onbuildNodesSuccess = function(objEvent) {
                var doc = tibco.admin.Main.getServer().getCache().getDocument("xmlBuildNodesResponse_30");
                var records = doc.selectNodes('//record[@success="false"]');
                if (records.size() == 0) {
                    // yellow fade out message for installing
                    this.yftPrompt(tibco.admin.Main.getServer().getDynamicProperty("@@InstallingMessage_30"));
                }

                this.dogetNodesInEnvironment();
            }

            plugin.prototype.onbuildNodesError = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.onbuildNodesInvalid = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.dostartNodes = function(parent) {
                if(this.nodeIds && (this.nodeIds.length > 0)) {
                    try {
                        var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/rules/startNodes.xml", "startNodes");
                        if(tibco.admin.Main.getServer().getEnv("MODE")) {
                            objService.setEndpointURL(tibco.admin.util.ServiceHelper.getNodeEndpointURL());
                        }

                        var nodeList = new jsx3.util.List(0);
                        var recordIter = this.matrixObj.getSelectedNodes().iterator();
                        while(recordIter.hasNext()) {
                            var record = recordIter.next();
                            var obj = new Object();
                            obj.nodeId = record.getAttribute("jsxid");
                            obj.nodeName = record.getAttribute("nodeName");
                            nodeList.add(obj, nodeList.size());
                        }
                        objService.nodesIter = nodeList.iterator();

                        //subscribe and call
                        var me = this;
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onstartNodesSuccess);
                        objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onstartNodesError);
                        objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onstartNodesInvalid);
                        objService.doCall();
                    }
                    catch(ex) {
                        tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.Environments.dostartNodes");
                    }
                }
            }

            plugin.prototype.onstartNodesSuccess = function(objEvent) {
                var doc = tibco.admin.Main.getServer().getCache().getDocument("xmlStartNodesResponse_30");
                var records = doc.selectNodes('//record[@success="false"]');
                if (records.size() == 0) {
                    // yellow fade out message for installing
                    this.yftPrompt(tibco.admin.Main.getServer().getDynamicProperty("@@StartingMessage_30"));
                }

                this.dogetNodesInEnvironment();
            }

            plugin.prototype.onstartNodesError = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.onstartNodesInvalid = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.dostopNodes = function(parent) {
                if(this.nodeIds && (this.nodeIds.length > 0)) {
                    try {
                        var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/rules/stopNodes.xml", "stopNodes");
                        if(tibco.admin.Main.getServer().getEnv("MODE")) {
                            objService.setEndpointURL(tibco.admin.util.ServiceHelper.getNodeEndpointURL());
                        }

                        var nodeList = new jsx3.util.List(0);
                        var recordIter = this.matrixObj.getSelectedNodes().iterator();
                        while(recordIter.hasNext()) {
                            var record = recordIter.next();
                            var obj = new Object();
                            obj.nodeId = record.getAttribute("jsxid");
                            obj.nodeName = record.getAttribute("nodeName");
                            nodeList.add(obj, nodeList.size());
                        }
                        objService.nodesIter = nodeList.iterator();

                        //subscribe and call
                        var me = this;
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onstopNodesSuccess);
                        objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onstopNodesError);
                        objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onstopNodesInvalid);
                        objService.doCall();
                    }
                    catch(ex) {
                        tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.Environments.dostopNodes");
                    }
                }
            }

            plugin.prototype.onstopNodesSuccess = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.onstopNodesError = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.onstopNodesInvalid = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.loadComponent = function(parent) {
                tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/environments/components/approve.xml", this, this.tabbedPaneLoaded);
                /*
                try {
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/rules/getEnvironments.xml", "getEnvironments");
                    if(tibco.admin.Main.getServer().getEnv("MODE")) {
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getEnvironmentEndpointURL());
                    }
                    objService.parentBlock = parent;

                    //subscribe and call
                    var me = this;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetEnvironmentsSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.ongetEnvironmentsError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.ongetEnvironmentsInvalid);
                    objService.doCall();
                }
                catch(ex) {
                    tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.Environments.loadComponent");
                }
                */
            }

            plugin.prototype.ongetEnvironmentsSuccess = function(objEvent) {
                var parent = objEvent.target.parentBlock;
                var me = this;
                tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/environments/components/selectEnvironment.xml", me, me.selectLoaded);
            }

            plugin.prototype.onremoveNodesSuccess = function(objEvent) {
                var iter = this.getPluginList().iterator();
                while(iter.hasNext()) {
                    var obj = iter.next();
                    try {
                        obj.setNodeIds(null);
                        obj.setPageType(this.type);
                    }
                    catch (ex) {
                        tibco.admin.Main.logException(ex);
                    }
                }
                this.nodeIds = null;
                this.setNodeNameLabel("");
                this.displayButtonPane(false);
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.displayButtonPane = function(display) {
                if(display) {
                    this.buttonPane.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    this.block.getDescendantOfName("blkMask_30").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                }
                else {
                    this.buttonPane.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.block.getDescendantOfName("blkMask_30").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                }
            }

            plugin.prototype.getComponentPath = function() {
                return "tibco/admin/environments/component.xml"
            }

            plugin.prototype.ongetNodesSuccess = function(objEvent) {
                if(this.buttonPane.getButtonPane().getChildren().length != 0) {
                    if(this.nodeIds) {
                        this.ensureNodesSelected(null, this.nodeIds);
                        this.nodesSelected(this.nodeIds);
                    }
                }
                this.matrixObj.repaint();
                this.updateButtons();
            }

            plugin.prototype.selectLoaded = function(objEvent) {
                // Use a hack way to customize the select box look like
                var selEnvironment = tibco.admin.Main.getServer().getJSXByName("selEnvironmentSelect_30");
                var selEnvironmentHTML = selEnvironment.getRendered();
                selEnvironmentHTML.firstChild.firstChild.style.paddingTop = 0;
                selEnvironmentHTML.style.backgroundImage = tibco.admin.Main.getServer().getDynamicProperty("@addin@Arrow For Header");
                selEnvironment.setDisplay(jsx3.gui.Block.DISPLAYBLOCK,true);

                if(objEvent.status == "SUCCESS") {
                    this.select = objEvent.obj.getChild(0);
                    var selXML = this.select.getXML();
                    var nodes = selXML.selectNodes("//record");

                    // if there is only one environment, then make it as workspace
                    // else, check the defalut worksapce variable.
                    var curJsxId = null;
                    if(nodes.size() == 1) {
                        curJsxId = nodes.getItem(0).getAttribute("jsxid")
                    }
                    else if (this.callbackTarget.workEnvironment) {
                        curJsxId = this.callbackTarget.workEnvironment;
                    }

                    if (curJsxId && selXML.selectSingleNode("//record[@jsxid='" + curJsxId + "']")) {
                        this.select.setValue(curJsxId);
                        this.itemSelected(curJsxId);
                    }
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            plugin.prototype.addNode = function() {
				APP.tibco.admin.getBodyBlock().load("tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/removePermissionsM.xml");
            }

            plugin.prototype.selectAll = function() {
				this.check = !this.check;
				var targetMatrix = this.block.getDescendantOfName("mtxApproves");
                var iter = targetMatrix.getXML().getChildIterator();
                while(iter.hasNext()) {
                    iter.next().setAttribute("checked", this.check)
                }
                targetMatrix.repaintData();
				this.block.getDescendantOfName("btnSelectAll_30").setText(this.check ? "全不选" : "全选", true);
            }

            plugin.prototype.ongetEnvironmentsError = function(objEvent) {
            }

            plugin.prototype.ongetEnvironmentsInvalid = function(objEvent) {
            }

            plugin.prototype.onremoveNodesError = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.onremoveNodesInvalid = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.ongetNodesError = function(objEvent) {
                this.ensureNodesSelected(null, this.nodeIds);
            }

            plugin.prototype.ongetNodesInvalid = function(objEvent) {
                this.ensureNodesSelected(null, this.nodeIds);
            }

            plugin.prototype.getSectionTitle = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@SectionTitle_30", this.envName);
            }

            plugin.prototype.getSecondarySelectionText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@SecondarySelectionText_30");
            }

            plugin.prototype.onAfterComponentUnloaded = function() {
                this.endEditMode();
                this.nodeIds = null;
                this.envId = null;
                this.envName = null;
                this.matrixObj = null;
                this.buttonPane = null;
                this.buttonPlugins = new Object();
                this.buttonstoLoad = new Object();
                this.currentPlugin = null;
                this.block = null;
            }

            plugin.prototype.loadStage2Component = function(parent, id) {
                this.envId = id;
                this.envName = this.select.getText();
                var tabbedPane = new com.gix.gui.PerspectiveTabbedPane("tabpaneEnvironmentTabs_30", 0, 0, "100%", "100%", null, this.getSectionTitle());
                parent.setChild(tabbedPane);
                tabbedPane.getChild(0).setRows("0,*",true);
                tibco.admin.Main.loadUIResourceAsync(tabbedPane.getContentPane(), this.getNamespace(), "tibco/admin/environments/components/approve.xml", this, this.tabbedPaneLoaded);
            }

            plugin.prototype.loadButton = function(objEvent) {
                var name = objEvent.target.getText();
                if(this.buttonsToLoad['button' + name]) {
                    var obj = this.buttonsToLoad['button' + name];
                    if(obj) {
                        obj.setEnvId(this.envId);
                        obj.setNodeIds(this.nodeIds);
						obj.setPageType(this.type);
                        obj.loadComponent(objEvent.target.getContentPane());
                        delete this.buttonsToLoad['button' + name];
                    }
                    this.currentPlugin = obj;
                }
                else {
                    var obj = this.buttonPlugins['button' + name];
                    if(obj) {
                        obj.buttonSelected();
                    }
                }
            }

            plugin.prototype.onAfterGetPlugins = function() {
                var targetMatrix = this.block.getDescendantOfName("mtxApproves");
                this.cacheDoc = targetMatrix.getXML().cloneDocument();
            }

            plugin.prototype.loadButtons = function(isAddMode) {
                this.displayButtonPane(true);
                var first = this.buttonPane.getButtonPane().getChildren().length == 0;
                if(first) {
                    var iter = this.getPluginList("tibco.admin.environments.ENV01").iterator();
                    while(iter.hasNext()) {
                        try {
                            var obj = iter.next();
                            if(this.nodeIds) {
                                obj.setNodeIds(this.nodeIds);
                            }
							obj.setPageType(this.type);
                            var button = this.buttonPane.addButton(obj.getButtonText());
                            if(first) {
                                first = false;
                                obj.isAddMode = isAddMode;
                                obj.loadComponent(button.getContentPane());
                            }
                            else {
                                this.buttonsToLoad['button' + obj.getButtonText()] = obj;
                            }
                            this.buttonPlugins['button' + obj.getButtonText()] = obj;
                        }
                        catch(ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                }
            }

            plugin.prototype.setupPlugin = function(plugin) {
                var me = this;
                plugin.addCompletedCallback = me.addCompleted;
            }

            plugin.prototype.deleteClicked = function(forceDelete) {
                var me = this;
                var yes = function(dialog) {
                    me.doremoveNodes(forceDelete);
                    dialog.doClose();
                }
                var no = function(dialog) {
                    me.dogetNodesInEnvironment();
                    dialog.doClose();
                }
                if(forceDelete) {
                    tibco.admin.Main.confirm(tibco.admin.Main.getServer().getDynamicProperty("@@DeleteFailedTitle_30"), tibco.admin.Main.getServer().getDynamicProperty("@@DeleteFailedMessage_30"), yes, no, tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Yes"), tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn No"), 2);
                }
                else {
                    tibco.admin.Main.confirm(tibco.admin.Main.getServer().getDynamicProperty("@@ConfirmTitle_30"), tibco.admin.Main.getServer().getDynamicProperty("@@ConfirmMessageDelete_30"), yes, no, tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Yes"), tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn No"), 2);
                }
            }

            plugin.prototype.uninstallClicked = function(forceUninstall) {

                var me = this;
                var yes = function(dialog) {
                    me.dodestroyNodes(forceUninstall);
                    dialog.doClose();
                }
                var no = function(dialog) {
                    me.dogetNodesInEnvironment();
                    dialog.doClose();
                }
                if(forceUninstall) {
                    tibco.admin.Main.confirm(tibco.admin.Main.getServer().getDynamicProperty("@@UninstallFailedTitle_30"), tibco.admin.Main.getServer().getDynamicProperty("@@UninstallFailedMessage_30"), yes, no, tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Yes"), tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn No"), 2);
                }
                else {
                    tibco.admin.Main.confirm(tibco.admin.Main.getServer().getDynamicProperty("@@ConfirmTitle_30"), tibco.admin.Main.getServer().getDynamicProperty("@@ConfirmMessageUninstall_30"), yes, no, tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Yes"), tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn No"), 2);
                }
            }

            //Uninstall Nodes
            plugin.prototype.dodestroyNodes = function(forceUninstall) {
                if(this.nodeIds && (this.nodeIds.length > 0)) {
                    try {
                        var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/rules/destroyNodes.xml", "destroyNodes");
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getNodeEndpointURL());

                        var nodeList = new jsx3.util.List(0);
                        var recordIter = this.matrixObj.getSelectedNodes().iterator();
                        while(recordIter.hasNext()) {
                            var record = recordIter.next();
                            var obj = new Object();
                            obj.nodeId = record.getAttribute("jsxid");
                            obj.nodeName = record.getAttribute("nodeName");
                            nodeList.add(obj, nodeList.size());
                        }
                        objService.nodesIter = nodeList.iterator();

                        //subscribe and call
                        var me = this;
                        objService.forceUninstall = forceUninstall;
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ondestroyNodesSuccess);
                        objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.ondestroyNodesError);
                        objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.ondestroyNodesInvalid);
                        objService.doCall();
                    }
                    catch(ex) {
                        tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.Environments.dodestroyNodes");
                    }
                }
            }

            plugin.prototype.ondestroyNodesSuccess = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.ondestroyNodesError = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.ondestroyNodesInvalid = function(objEvent) {
                this.dogetNodesInEnvironment();
            }

            //Delete Nodes
            plugin.prototype.doremoveNodes = function(forceDelete) {
                if(this.nodeIds && (this.nodeIds.length > 0)) {
                    var list = this.block.getDescendantOfName("mtxNodes_30").getSelectedNodes();
                    var cdfList = jsx3.xml.CDF.Document.newDocument();
                    var iter = list.iterator();
                    while(iter.hasNext()) {
                        try {
                            var obj = iter.next();
                            cdfList.appendChild(obj.cloneNode(true));
                        }
                        catch (ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    var server = tibco.admin.Main.getServer();
                    server.getCache().setDocument("cdfRemoveNodes_30", cdfList);

                    try {
                        var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/rules/removeNodes.xml", "removeNodes");
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getNodeEndpointURL());

                        //subscribe and call
                        var me = this;
                        objService.forceDelete = forceDelete;
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onremoveNodesSuccess);
                        objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onremoveNodesError);
                        objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onremoveNodesInvalid);
                        objService.doCall();
                    }
                    catch(ex) {
                        tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.Environments.doremoveNodes");
                    }
                }
            }

            plugin.prototype.addCompleted = function(selectedNodeId) {
                if(selectedNodeId) {
                    this.nodeIds = [selectedNodeId];
                }
                var childArr = this.buttonPane.getButtonPane().getChildren()
                for(var i = 1; i < childArr.length; i++) {
                    childArr[i].setEnabled(jsx3.gui.Form.STATEENABLED, true);
                }
                this.dogetNodesInEnvironment();
            }

            plugin.prototype.setNodeNameLabel = function(strName) {
                if(this.buttonPane) {
                    this.buttonPane.setLabelText(strName);
                }
            }

            plugin.prototype.nodesSelected = function(arrNodeIds) {
                this.loadButtons(false);
            }

            plugin.prototype.ensureNodesSelected = function(arrDeselect, arrNodeIds) {
                this.ignoreSelects = true;
                if(arrDeselect) {
                    for(var i = 0; i < arrDeselect.length; i++) {
                        this.matrixObj.deselectRecord(arrDeselect[i]);
                    }
                }
                if(arrNodeIds) {
                    for(var j = 0; j < arrNodeIds.length; j++) {
                        this.matrixObj.selectRecord(arrNodeIds[j]);
                    }
                }
                this.ignoreSelects = false;
            }

            plugin.prototype.updateButtons = function() {
                var list = this.matrixObj.getSelectedNodes();
                var count = list.size();
                var defined = 0
                var installed = 0
                var installedStopped = 0
                var installFailed = 0
                var installing = 0
                var uninstalling = 0;
                var uninstallFailed = 0;
                var startFailed = 0;
                var stopFailed = 0;
                var starting = 0;
                var stopping = 0;
                var running = 0;
                var deleteFailed = 0;
                var unknown = 0;
                var notOwner = 0;
                var iter = list.iterator();
                while(iter.hasNext()) {
                    var node = iter.next();
                    var state = node.getAttribute("state");
                    if(node.getAttribute("hasOwner") != "true") {
                        notOwner++;
                    }
                    switch(state) {
                        case "Defined":
                            defined++;
                            break;
                        case "Installed":
                            installed++;
                            break;
                        case "Installed/Stopped":
                            installedStopped++;
                            break;
                        case "Install Failed":
                            installFailed++;
                            break;
                        case "Installing":
                            installing++;
                            break;
                        case "Uninstalling":
                            uninstalling++;
                            break;
                        case "Uninstall Failed":
                            uninstallFailed++;
                            break;
                        case "Start Failed":
                            startFailed++;
                            break;
                        case "Stop Failed":
                            stopFailed++;
                            break;
                        case "Starting":
                            starting++;
                            break;
                        case "Stopping":
                            stopping++;
                            break;
                        case "Running":
                            running++;
                            break;
                        case "Delete Failed":
                            deleteFailed++;
                            break;
                        case "Unknown":
                            unknown++;
                            break;
                        default:
                            unknown++;
                            break;
                    }
                }
                tibco.admin.Main.getServer().getJSXByName("btnAdd_30").setEnabled(!this.isInEditMode(), true);
            }

            plugin.prototype.dogetNodesInEnvironment = function() {
                try {
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/rules/getNodesInEnvironment.xml", "getNodesInEnvironment");
                    if(tibco.admin.Main.getServer().getEnv("MODE")) {
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getNodeEndpointURL());
                    }

                    //subscribe and call
                    var me = this;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetNodesSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.ongetNodesError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.ongetNodesInvalid);
                    objService.doCall();
                }
                catch(ex) {
                    tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.Environments.dogetNodesInEnvironment");
                }
            }

            plugin.prototype.tabbedPaneLoaded = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    //this.dogetNodesInEnvironment();
                    this.block = objEvent.obj;
                    var me = this;
                    this.block.handler = me;
                    this.matrixObj = this.block.getDescendantOfName("mtxNodes_30");
                    this.buttonPane = this.block.getDescendantOfName("paneContent_30");
                    this.buttonPane.subscribe(com.gix.gui.LeftSelectPane.BUTTON_SELECTED, this, this.loadButton)

                    if(this.getPluginList().size() == 0) {
                        var preLoad = new jsx3.util.List(0);
/*
                        if(!tibco.admin.environments.genDtls || !tibco.admin.environments.genDtls.GenDtls) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/genDtls/GenDtls.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.genDtls.GenDtls, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.genDtls.GenDtls, preLoad.size());
                        }

                        if(!tibco.admin.environments.defConn || !tibco.admin.environments.defConn.DefConn) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/defConn/DefConn.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.defConn.DefConn, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.defConn.DefConn, preLoad.size());
                        }

                        if(!tibco.admin.environments.containers || !tibco.admin.environments.containers.Containers) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/containers/Containers.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.containers.Containers, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.containers.Containers, preLoad.size());
                        }

                        if(!tibco.admin.environments.shdReso || !tibco.admin.environments.shdReso.ShdReso) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/shdReso/ShdReso.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.shdReso.ShdReso, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.shdReso.ShdReso, preLoad.size());
                        }

                        // load Local Substitution Variables
                        if(!tibco.admin.environments.LocalSvarsPlugin || !tibco.admin.environments.localSvars.LocalSvarsPlugin) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/localSvars/LocalSvarsPlugin.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.localSvars.LocalSvarsPlugin, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.localSvars.LocalSvarsPlugin, preLoad.size());
                        }

                        if(!tibco.admin.environments.logCfg || !tibco.admin.environments.logCfg.LogCfg) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/logCfg/LogCfg.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.logCfg.LogCfg, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.logCfg.LogCfg, preLoad.size());
                        }


                        if(!tibco.admin.environments.keystore || !tibco.admin.environments.keystore.KeystorePlugin) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/keystore/KeystorePlugin.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.keystore.KeystorePlugin, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.keystore.KeystorePlugin, preLoad.size());
                        }

                        */

						// load Loan Infomation 
                        if(!tibco.admin.environments.LoanInfoPlugin || !tibco.admin.environments.loanInfo.LoanInfoPlugin) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/loanInfo/LoanInfoPlugin.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.loanInfo.LoanInfoPlugin, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.loanInfo.LoanInfoPlugin, preLoad.size());
                        }

						
						// load Base Infomation 
                        if(!tibco.admin.environments.BaseInfoPlugin || !tibco.admin.environments.baseInfo.BaseInfoPlugin) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/baseInfo/BaseInfoPlugin.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.baseInfo.BaseInfoPlugin, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.baseInfo.BaseInfoPlugin, preLoad.size());
                        }
                        
                        // load Account Infomation 
                        if(!tibco.admin.environments.AccountInfoPlugin || !tibco.admin.environments.accountInfo.AccountInfoPlugin) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/accountInfo/AccountInfoPlugin.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.accountInfo.AccountInfoPlugin, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.accountInfo.AccountInfoPlugin, preLoad.size());
                        }
						
						// load TogeLoan Infomation 
                        if(!tibco.admin.environments.TogeLoanInfoPlugin || !tibco.admin.environments.togeLoanInfo.TogeLoanInfoPlugin) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/togeLoanInfo/TogeLoanInfoPlugin.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.togeLoanInfo.TogeLoanInfoPlugin, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.togeLoanInfo.TogeLoanInfoPlugin, preLoad.size());
                        }
                        
						// load School Infomation 
                        if(!tibco.admin.environments.SchoolInfoPlugin || !tibco.admin.environments.schoolInfo.SchoolInfoPlugin) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/schoolInfo/SchoolInfoPlugin.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.schoolInfo.SchoolInfoPlugin, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.schoolInfo.SchoolInfoPlugin, preLoad.size());
                        }


                        this.loadPlugins("tibco.admin.environments.EnvironmentsInterface", preLoad);
                    }
                    else {
                        this.onAfterGetPlugins();
                    }
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            plugin.prototype.itemSelected = function(id) {
                if(!this.ignoreSelects) {
                    this.lastId = this.currentId;
                    this.currentId = id;
                    if(this.isInEditMode()) {
                        var me = this;
                        this.allowComponentUnload(me,
                                function() {
                                    me.unload();
                                    me.notifyParent(id);
                                },
                                function() {
                                    me.ignoreSelects = true;
                                    me.select.setValue(me.lastId);
                                    me.currentId = me.lastId;
                                    me.lastId = null;
                                    me.ignoreSelects = false;
                                }, id);
                    }
                    else {
                        this.notifyParent(id);
                    }
                }
            }

            plugin.prototype.beginEdit = function(objPlugin) {
                var button = this.findButton(objPlugin);
                if(button) {
                    this.buttonPane.setEditMode(button, true);
                }
                this.updateButtons();
            }

            plugin.prototype.endEdit = function(objPlugin) {
                var button = this.findButton(objPlugin);
                if(button) {
                    this.buttonPane.setEditMode(button, false);
                }
                this.updateButtons();
                if(this.matrixObj.getSelectedNodes().size() == 0) {
                    this.displayButtonPane(false);
                }
            }

            plugin.prototype.findButton = function(objPlugin) {
                try {
                    var buttons = this.buttonPane.getButtonPane().getChildren();
                    for(var i = 0; i < buttons.length; i++) {
                        var child = buttons[i];
                        if(child.getText() == objPlugin.getButtonText()) {
                            return child;
                        }
                    }
                }
                catch(ex) {
                    // the component was unloaded, most likely.  just return null;
                }
                return null;
            }

            /**
             * Load permission buttons from a serialize file
             */
            plugin.prototype.loadPermissionButtons = function() {
                var domNode = this.block.getDescendantOfName("panePermissions_30");
                this.permissions = tibco.admin.Main.loadUIResource(domNode, "tibco.admin",
                        "tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/setPermissions.xml", true);
                this.permissions.initialize("MatrixNode", "jsxid", "nodeName", this.matrixObj);
            };

			plugin.prototype.setSelectDefaultText = function() {
                tibco.admin.Main.getServer().getJSXByName("selEnvironmentSelect_30").setDefaultText(tibco.admin.Main.getServer().getDynamicProperty("@@selectDefaultText"));
            };

        });


// Yellow Fade Out for GI component.
var yft = {
    _steps: 15,
    _interval: 200,
    _stopFade: false,
    _element: null,
    _shade: {0:"eca0", 1:"edaf", 2:"eea9", 3:"efae", 4:"f0b3", 5:"f1b8", 6:"f2bc", 7:"f3c1", 8:"f4c6", 9:"f5cb", 10:"f5cf", 11:"f6d4", 12:"f7d9", 13:"f9e2", 14:"fbec", 15:"fdf5"},
    _text: {0:"00", 1:"11", 2:"22", 3:"33", 4:"44", 5:"55", 6:"66", 7:"77", 8:"88", 9:"99", 10:"aa", 11:"bb", 12:"cc", 13:"dd", 14:"ee", 15:"ff"},
    fadeOut: function(elem,i) {
        if (!this._stopFade && i <= this._steps) {
            elem.setBackgroundColor("#ff" + this._shade[i], true);
            elem.setColor("#"+ this._text[i] + this._text[i] + this._text[i], true);

            if (i == this._steps) {
                elem.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
            }

            window.setTimeout(function(){yft.fadeOut(elem,++i)}, this._interval);
        }
    },
    startFadeOut: function(elem, steps, interval) {
        if (interval) {
            this._interval = Math.max(interval, 0);
        }
        if (steps) {
            this._steps = Math.max(Math.min(steps, this._steps), 0);
        }
        this._stopFade = false;
        this._element = elem;
        elem.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
        this.fadeOut(elem, 0);
    },
    stopFadeOut: function() {
        if (this._element)
        {
            this._stopFade = true;
            this._element.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
        }
    }
}

